package org.kjtc.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.kjtc.entity.*;

public class WeChatProvider {

    public String getWechatUser(WechatUser wechatUser) {
        String newSql = " * from wechat.wechatuser ";
        SQL sql = new SQL().SELECT(newSql);
        if (wechatUser != null && !StringUtils.isEmpty(wechatUser.getUserID())) {
            sql.WHERE("UserID in (" + wechatUser.getUserID() + ") ");
        }
        if (wechatUser != null && !StringUtils.isEmpty(wechatUser.getWeChatID())) {
		 	sql.WHERE("WeChatID = #{WeChatID} ");
		}
		sql.ORDER_BY("UpdateDTTM desc");
        return sql.toString();
    }

    public String getSendUser(WechatUser wechatUser) {
        String newSql = " * from wechat.wechatuser where 1 = 1";
        newSql += " and ( UserID in (" + wechatUser.getUserID() + ") or WeChatID in (" + wechatUser.getWeChatID() + ") )";
        SQL sql = new SQL().SELECT(newSql);
		sql.ORDER_BY("UpdateDTTM desc");
        return sql.toString();
    }
    public String syncWechatUser(WechatUser wechatUser) {
        String newSql = "wechat.wechatuser " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE(), ";
            if (wechatUser != null && !StringUtils.isEmpty(wechatUser.getWeChatNickName())) {
                newSql += "WeChatNickName = #{WeChatNickName}, ";
            }
            if (wechatUser != null && !StringUtils.isEmpty(wechatUser.getWeChatImageUrl())) {
                newSql += "WeChatImageUrl = #{WeChatImageUrl}, ";
            }
            if (wechatUser != null && wechatUser.getSubscribeTime() != null) {
                newSql +=  "SubscribeTime = #{SubscribeTime}, ";
            }
            if (wechatUser != null && wechatUser.getUnsubscribeTime() != null) {
                newSql +=  "UnsubscribeTime = #{UnsubscribeTime}, ";
            }
            newSql += "Enabled = #{Enabled} " +
                    "where " +
                    "WeChatID = #{WeChatID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String getWechatMessagePage(WechatMessage wechatMessage) {
        String newSql = " * from wechat.wechatmessage ";
        SQL sql = new SQL().SELECT(newSql);
        if (wechatMessage != null && wechatMessage.getID()> 0) {
            sql.WHERE("ID = " + wechatMessage.getID());
        }
        sql.ORDER_BY("CreateDTTM desc");
        return sql.toString();
    }

    public String getWechatMessage(WechatMessage wechatMessage) {
        String newSql = " * from wechat.wechatmessage ";
        SQL sql = new SQL().SELECT(newSql);
        if (wechatMessage != null && wechatMessage.getID()> 0) {
            sql.WHERE("ID = " + wechatMessage.getID());
        }
        sql.ORDER_BY("UpdateDTTM desc ");
        return sql.toString();
    }
	public String saveMenuSure(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu  " +
                "(MenuName, " +
                " MenuKey, " +
                " Type, " +
                " Url, " +
                " Level, " +
                " OrderNumber, " +
                " AppID, " +
                " PagePath, " +
                " Enabled, " +
                " ParentMenuID, " +
                " CreateUser, " +
                " CreateDTTM, " +
                " UpdateUser, " +
                " UpdateDTTM) VALUES(" +
                "#{MenuName}, " +
                "#{MenuKey}, " +
                "#{Type}, " +
                "#{Url}, " +
                "#{Level}, " +
                "(select IFNULL(OrderNumber,1) from (select max(IFNULL(OrderNumber,1))+1 as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID})w ), " +
                "#{AppID}, " +
                "#{PagePath}, " +
                "#{Enabled}, " +
                "#{ParentMenuID}, " +
                "#{CreateUser}, " +
                "SYSDATE(), " +
                "#{UpdateUser}, " +
                "SYSDATE()) ";
        SQL sql = new SQL().INSERT_INTO(newSql);
        return sql.toString();
    }

    public String insertWechatMenu(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu  " +
                "(MenuName, " +
                " MenuKey, " +
                " Type, " +
                " Url, " +
                " Level, " +
                " OrderNumber, " +
                " AppID, " +
                " PagePath, " +
                " Enabled, " +
                " ParentMenuID, " +
                " CreateUser, " +
                " CreateDTTM, " +
                " UpdateUser, " +
                " UpdateDTTM) VALUES(" +
                "#{MenuName}, " +
                "#{MenuKey}, " +
                "#{Type}, " +
                "#{Url}, " +
                "#{Level}, ";
        if (StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql +="(select OrderNumber from (select max(IFNULL(OrderNumber,1))+1 as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID})w ), ";
        } else if (!StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql +="(select OrderNumber from (select IFNULL(OrderNumber,1) as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID} and MenuID = #{ChangeMenuID})w ), ";
        }
        newSql += "#{AppID}, " +
                "#{PagePath}, " +
                "#{Enabled}, " +
                "#{ParentMenuID}, " +
                "#{CreateUser}, " +
                "SYSDATE(), " +
                "#{UpdateUser}, " +
                "SYSDATE()) ";
        SQL sql = new SQL().INSERT_INTO(newSql);
        return sql.toString();
    }

    public String updateOrderNum(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "OrderNumber = (select OrderNumber from (select max(IFNULL(OrderNumber,1))+1 as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID})w ) " +
                "where " +
                "MenuID = #{MenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateWechatMenu(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE(), " +
                "MenuName = #{MenuName}, " +
                "MenuKey = #{MenuKey}, " +
                "Url = #{Url}, " +
                "Type = #{Type}, " +
                "Level = #{Level}, " +
                "AppID = #{AppID}, " +
                "PagePath = #{PagePath}, " +
                "Enabled = #{Enabled} ";
        if (!StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql += ", OrderNumber = (select OrderNumber from (select IFNULL(OrderNumber,1) as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID} and MenuID = #{ChangeMenuID})w ) ";
        }
        newSql += " where " +
                "MenuID = #{MenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateMoveMenu(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE() " ;
        if (!StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql += ",OrderNumber = (select IFNULL(OrderNumber,1) as OrderNumber from (select IFNULL(OrderNumber,1) as OrderNumber from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID} and MenuID = #{ChangeMenuID})w ) ";
        }
        newSql += " where MenuID = #{MenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateMoveMenuSure(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE() " ;
        if (!StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql += ",OrderNumber = IFNULL(#{OrderNumber},1) ";
        }
        newSql += " where MenuID = #{ChangeMenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateMenuSure(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE(), " +
                "MenuName = #{MenuName}, " +
                "MenuKey = #{MenuKey}, " +
                "Url = #{Url}, " +
                "Type = #{Type}, " +
                "AppID = #{AppID}, " +
                "PagePath = #{PagePath}, " +
                "Enabled = #{Enabled} " +
                " where " +
                "MenuID = #{MenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateWechatSure(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE(), " +
                "OrderNumber = IFNULL(#{OrderNumber},1) " +
                " where " +
                "MenuID = #{ChangeMenuID} ";
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String updateWeChatMenuSure(WechatMenu wechatMenu) {
        String newSql = "wechat.wechatmenu " +
                "set " +
                "UpdateUser = #{UpdateUser}, " +
                "UpdateDTTM = SYSDATE() " ;
        if (!StringUtils.isEmpty(wechatMenu.getChangeMenuID())) {
            newSql += ", OrderNumber = IFNULL(#{OrderNumber},1) from wechat.wechatmenu  where ParentMenuID = #{ParentMenuID} and MenuID = #{MenuID} ";
        }
        SQL sql = new SQL().UPDATE(newSql);
        return sql.toString();
    }

    public String getWechatConfig(WechatConfig wechatConfig) {
        String newSql = " * from wechat.wechatconfig ";
        SQL sql = new SQL().SELECT(newSql);
        if (wechatConfig != null && !StringUtils.isEmpty(wechatConfig.getType())) {
            sql.WHERE("Type = #{Type} ");
        }
        return sql.toString();
    }

}
